﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using TheOutfield.UmbExt.UniversalMediaPicker.Xslt;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using umbraco;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Vimeo
{
    public class VimeoXsltExtension : AbstractXsltExtension
    {
        public static XPathNodeIterator GetMedia(int docTypeDefinitionId, int id)
        {
            var service = GetProvider<VimeoProvider>(docTypeDefinitionId).Service;

            try
            {
                var video = service.GetVideo(id);

                var xd = new XmlDocument();
                var docNode = xmlHelper.addTextNode(xd, "Video", "");

                if (video != null)
                {
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Id", video.Id.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Url", video.Url));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Title", video.Title));
                    docNode.AppendChild(xmlHelper.addCDataNode(xd, "Description", video.Description));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailSmall", video.ThumbnailSmall));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailMedium", video.ThumbnailMedium));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "ThumbnailLarge", video.ThumbnailLarge));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Duration", video.Duration.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Width", video.Width.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Height", video.Height.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "Tags", video.Tags));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "UploadDate", video.UploadDate.ToString("yyyy-MM-dd HH:mm:ss")));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "UserName", video.UserName));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "UserUrl", video.UserUrl));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "UserPortraitSmall", video.UserPortraitSmall));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "UserPortraitMedium", video.UserPortraitMedium));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "UserPortraitLarge", video.UserPortraitLarge));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "StatsNumberOfLikes", video.StatsNumberOfLikes.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "StatsNumberOfPlays", video.StatsNumberOfPlays.ToString()));
                    docNode.AppendChild(xmlHelper.addTextNode(xd, "StatsNumberOfComments", video.StatsNumberOfComments.ToString()));
                    docNode.AppendChild(xmlHelper.addCDataNode(xd, "EmbedCode", service.GetVideoEmbedCode(id)));
                }

                xd.AppendChild(docNode);

                return xd.CreateNavigator().Select("/Video");
            }
            catch (Exception e)
            {
                return e.ToXPathNodeIterator();
            }
        }

        public static string GetEmbedCode(int docTypeDefinitionId, 
            int id, 
            int width,
            int maxWidth,
            int height,
            int maxHeight,
            bool byline,
            bool title,
            bool portrait,
            string color,
            bool autoPlay,
            bool xhtml,
            string wmode,
            bool iframe)
        {
            var service = GetProvider<VimeoProvider>(docTypeDefinitionId).Service;
            return service.GetVideoEmbedCode(id, 
                width,
                maxWidth,
                height,
                maxHeight,
                byline,
                title,
                portrait,
                color,
                autoPlay,
                xhtml,
                wmode,
                iframe);
        }
    }
}